<?php

class Articles extends CI_Controller {

    public function __construct()
	{
		parent::__construct();
		$this->load->model('Blog_model', 'blog');
	}
	
	//the home page of the blog
	public function index($offset=0)
	{
		$entries['active'] = 'article';
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		$sort =array( 
			array(
				'column'=>'creation_date',
				'order' => 'desc'
				),
			array(
				'column' => 'summary',
				'order' => 'asc'
				),
		 );
		
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('articles/index');
		$config['total_rows'] = (int)$this->blog->count_all();
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$config['display_pages'] = FALSE;
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link']='Previous';
		$config['next_link']='Next';
		$this->pagination->initialize($config);
		$entries['pagination'] = $this->pagination->create_links();
		
		
		$entries['query'] = $this->blog->get_all_entries($cond,$sort, $limit, $offset);
		$entries['archives'] = $this->blog->get_archive();
		$this->load->view('articles/index', $entries);
	}
	//browse by month and year
	public function browse($searchby = null, $offset=0){
		if ($searchby == null)
			$searchby = date('Y') . '_'. date('M');

		$entries['active'] = 'article';
		$cond['is_active'] = 1;
		$limit = $this->config->item('per_page');
		$keywords = explode('_', $searchby);
		$cond['year'] = $keywords[0];
		$cond['month'] = $keywords[1];
		$sort =array( 
			array(
				'column'=>'creation_date',
				'order' => 'desc'
				),
			array(
				'column' => 'summary',
				'order' => 'asc'
				),
		 );
		//pagination
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = site_url('articles/browse/') . '/' . $searchby . '/';
		$config['total_rows'] = (int)$this->blog->count_all($cond);
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$config['display_pages'] = FALSE;
		$config['first_link'] = FALSE;
		$config['last_link'] = FALSE;
		$config['prev_link']='Previous';
		$config['next_link']='Next';
		$this->pagination->initialize($config);
		$entries['pagination'] = $this->pagination->create_links();
		
		
		$entries['query'] = $this->blog->get_all_entries($cond,$sort, $limit, $offset);
		$entries['archives'] = $this->blog->get_archive();
		$this->load->view('articles/index', $entries);
	}

	//For an individual entry, the url title is used to grab 
	//the entry
	public function entry($url_title = "")
	{
		  $this->load->helper('url');
		  
		  if($url_title){
			
			$entry_data['post'] = $this->blog->get_entry($url_title);
			
			if(!$entry_data['post']){
			  redirect('/articles', 'location');
			} else {
			  $this->load->view('articles/entry', $entry_data);
			}
		 } else {
		   redirect('/articles', 'location');
		 }
	}
	
}

/* End of file articles.php */
/* Location: ./controllers/admin/articles.php */ 

?>
